{% extends 'member-add.html' %}

{% block head %}
<style type="text/css">
    .display_none{display: none;}
</style>


{% endblock %}

{% block bodyweigh %}

{% endblock %}

{% block content %}
    <style type="text/css">
    .display_none{display: none;}
    </style>
<!-- 右侧主体开始 -->
    <div class="page-content">
      <div class="content">
        <!-- 右侧内容框架，更改从这里开始 -->
        <form class="layui-form" >
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>赛事主题
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="name1" 
                    autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item" >
                <label for="L_pass" class="layui-form-label" >
                    <span class="x-red">*</span>赛事类型
                </label>
                <div class="layui-input-inline" id="add_type_id">
                    <select>
                        <option value ="1">官方赛</option><option value ="2">平台赛</option>
                        <option value="3">业余赛</option>
                    </select>
                    
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label" >
                    <span class="x-red">*</span>赛事类型
                </label>
                <div class="layui-input-inline" id="add_hot_status">
                    <select><option value ="0">普通赛事</option><option value ="1">热门赛事</option></select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>奖品类型
                </label>
                <div class="layui-input-inline" id="add_award_type">
                    <select>
                        <option value ="1">人民币</option>
                        <option value ="2">平台币</option>
                        <option value ="3">实物</option>
                        <option value ="4">游戏礼包</option>
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">   
                    <span class="x-red">*</span>游戏类型
                </label>
                <div class="layui-input-inline" id="add_game_type">
                   <select></select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>


            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>1等奖
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="1" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>2等奖
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="2" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>3等奖
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="3" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <div id="more">继续添加奖品</div>
                </div>
            </div>

            <div class="layui-form-item" id="introduce">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>赛事介绍
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name4" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>赛事规则
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name4" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>队伍最大总数
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name17" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>一支队伍最多人数
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name"  autocomplete="off" class="layui-input" id="name18" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>报名开始时间
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name5" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    填写格式请参照： 2018-07-03 15:50:23
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>报名截止时间
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name6" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red"></span>赛事开始时间
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name7" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red"></span>赛事结束时间
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name8" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>赛事图片
                </label>
                <div class="layui-input-inline">
                    <input type="file" class="addimage" accept="image/*"/><input type="hidden" class="addimgurl" value=""/>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red"></span>
                </label>
                <div class="layui-input-inline">
                    <button type="button" lay-filter="add" lay-submit style="background-color: #009688;border: none;color: #fff; width:80px;height:38px" class="addition layui-btn">添加</button>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
<!--                 <button class="layui-btn addition" lay-filter="add" lay-submit="">
                        添加
                </button> -->
            </div>
        <!-- 右侧内容框架，更改从这里结束 -->
      </div>
    </div>
    <!-- 右侧主体结束 -->

{% endblock %}

{% block script %}
    <script>


    function strDateTime(str) {
        var reg = /^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/;
        var r = str.match(reg);
        if(r==null) {
            return false;
        }else {
            return str;
        }
    }


    {% for type in game_types %}

        $("#add_game_type select").append('<option value="{{loop.index}}">{{type}}</option>')
    
    {% endfor %}

    $(".addimage").change(function(){
        var imageurl = $(this).next();
        var file = this.files[0];
        if (window.FileReader) {
                var reader = new FileReader();
                reader.readAsDataURL(file);
                //监听文件读取结束后事件
              reader.onloadend = function (e) {
                imageurl.val(e.target.result);    //e.target.result就是最后的路径地址
                };
           }
    });


    $("#more").on("click",function(){

        var num_str = $("#introduce").prev().find("input").attr("id");
        var num = parseInt(num_str) + 1;

        $("#introduce").before('<div class="layui-form-item">'+
                '<label for="L_pass" class="layui-form-label">'+
                    '<span class="x-red"></span>'+num+'等奖'+
                '</label>'+
                '<div class="layui-input-inline">'+
                    '<input type="text" name="name" autocomplete="off" class="layui-input" id="'+num+'" value="" />'+
                '</div>'+
                '<div class="layui-form-mid layui-word-aux">'+
                    
                '</div>'+
            '</div>');

    });



    $(".addition").on("click",function(){

        var last_num = parseInt($("#introduce").prev().find("input").attr("id"));
        var prize = {}

        for(var i=1;i<last_num+1;i++){

             
                prize[(i)] = $("#"+i).val();
            
        }


        var add_type_id = $("#add_type_id").find(".layui-this").attr("lay-value");
        var add_hot_status = $("#add_hot_status").find(".layui-this").attr("lay-value");
        var add_game_type = $("#add_game_type").find(".layui-this").attr("lay-value");
        var add_award_type = $("#add_award_type").find(".layui-this").attr("lay-value");
        var title = $("#name1").val();
        var type = add_type_id;
        var hot_status = add_hot_status;
        var award_type = add_award_type;
        var rule = $("#name4").val();
        var apply_stime = $("#name5").val();
        var apply_etime = $("#name6").val();
        var game_stime = $("#name7").val();
        var game_etime = $("#name8").val();
        var introduce = $("#name10").val();
        var image = $(".addimgurl").val();
        var game_type = add_game_type;
        var max_teams = $("#name17").val();
        var team_num = $("#name18").val();

        var first = $("#1").val();
        var second = $("#2").val();
        var third = $("#3").val();



        var time1 = strDateTime(apply_stime);
        var time2 = strDateTime(apply_etime);
        var time3 = strDateTime(game_stime);
        var time4 = strDateTime(game_etime);

        if ( title == "" ) {
                alert("赛事主题的值不能为空！");
                return false;
            }
        if ( type == "" ) {
            alert("赛事类型的值不能为空！");
            return false;
        }

        if ( first == "" ) {
            alert("1等奖的值不能为空！");
            return false;
        }
        if ( second == "" ) {
            alert("2等奖的值不能为空！");
            return false;
        }
        if ( third == "" ) {
            alert("3等奖的值不能为空！");
            return false;
        }

        if ( introduce == "" ) {
            alert("赛事介绍的值不能为空！");
            return false;
        }
        if ( rule == "" ) {
            alert("赛事规则的值不能为空！");
            return false;
        }
        if ( max_teams == "" ) {
            alert("队伍最大总数的值不能为空！");
            return false;
        }
        if ( team_num == "" ) {
            alert("一支队伍最多人数的值不能为空！");
            return false;
        }

        if (time1 == false || apply_stime == "") {
            alert('请检查时间格式是否符合:2018-07-03 15:50:23，且报名开始时间不能为空');
            return false;
        }
        if (time2 == false || apply_stime == "") {
            alert('请检查时间格式是否符合:2018-07-03 15:50:23，且报名结束时间不能为空');
            return false;
        }

        if (game_stime == "") {
        } else {
            if (time3 == false) {
                alert('请检查时间格式是否符合:2018-07-03 15:50:23');
                return false;
            }
        }
        if (game_etime == "") {
        } else {
            if (time4 == false) {
                alert('请检查时间格式是否符合:2018-07-03 15:50:23');
                return false;
            }
        }

        if (game_stime != "" && game_etime == ""){
            alert('赛事开始时间和赛事结束时间必须同时添加');
            return false;
        }
        if (game_etime != "" && game_stime == ""){
            alert('赛事开始时间和赛事结束时间必须同时添加');
            return false;
        }
            
        if (image == ""){
            alert('请添加赛事图片');
            return false;
        }


        params = {
            "title":title,
            "type":type,
            "rule":rule,
            "icon":image,
            "apply_stime":apply_stime,
            "apply_etime":apply_etime,
            "game_stime":game_stime,
            "game_etime":game_etime,
            "award_type":award_type,
            "introduce":introduce,
            "hot_status":hot_status,          
            "game_type":game_type,
            "max_teams":max_teams,
            "team_num":team_num,
            
            "prize":prize,
        };

        $.ajax({
            url:"/app/event/add",
            type:"POST",
            data:JSON.stringify(params),
            contentType:'application/json',
            success:function(res){
                if(res.status == 0){
                    alert("信息添加成功！");      
                    location.href="/app/show/add_event";                

                }else {
                    alert(res.msg);
                }
            }
        })
    });
        


    </script>   
{% endblock %}